package com.accesshollywood.grfxOrdering.panels;

import java.awt.BorderLayout;
import java.awt.Color;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.EtchedBorder;

import org.jdesktop.swingx.JXTitledSeparator;
import org.jdesktop.swingx.VerticalLayout;

import com.accesshollywood.components.FileFolderLink;

public class AbstractTitledPanel extends JPanel {
	private JXTitledSeparator		titleLeft	= new JXTitledSeparator();
	private JXTitledSeparator		titleRight	= new JXTitledSeparator();
	private JXTitledSeparator		noLink		= new JXTitledSeparator("(Drag and Drop folder here)");
	private FileFolderLink			link		= new FileFolderLink();
	private JPanel					titlePanel	= new JPanel();
	JPanel							leftPanel	= new JPanel();
	JPanel							centerPanel	= new JPanel();
	JPanel							rightPanel	= new JPanel();

	private PropertyChangeSupport	mPcs		= new PropertyChangeSupport(this);

	public AbstractTitledPanel() {
		super();
		//titleLeft.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
		//titleRight.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
		titleLeft.setHorizontalAlignment(SwingConstants.CENTER);
		titleRight.setHorizontalAlignment(SwingConstants.CENTER);

		noLink.setHorizontalAlignment(SwingConstants.CENTER);
		link.setHorizontalAlignment(SwingConstants.CENTER);

		titlePanel.setLayout(new BoxLayout(titlePanel, BoxLayout.X_AXIS));

		leftPanel.add(titleLeft);
		rightPanel.add(titleRight);
		
		titlePanel.add(leftPanel, BorderLayout.WEST);
		titlePanel.add(centerPanel, BorderLayout.CENTER);
		titlePanel.add(rightPanel, BorderLayout.EAST);

		leftPanel.setLayout(new VerticalLayout());
		centerPanel.setLayout(new VerticalLayout());
		rightPanel.setLayout(new VerticalLayout());
		
		//leftPanel.setBorder(new EtchedBorder());
		//centerPanel.setBorder(new EtchedBorder());
		//rightPanel.setBorder(new EtchedBorder());
//		setBorder(new EtchedBorder());
		setBorder(BorderFactory.createMatteBorder(1, 2, 1, 2, Color.black));
		
		add(titlePanel, 0);
		refreshTitle();
	}

	public String getTitle() {
		return titleLeft.getTitle();
	}

	public void setTitle(String title) {
		this.titleLeft.setTitle(title);
		this.titleRight.setTitle(title);
		refreshTitle();
	}

	public void setLink(String str) {
		link.setLink(str);
		refreshTitle();
	}

	public void refreshTitle() {
		if (link.isActive) {
			centerPanel.remove(noLink);
			centerPanel.add(link);
		} else {
			centerPanel.remove(link);
			centerPanel.add(noLink);
		}
		titlePanel.revalidate();
		titlePanel.repaint();
	}

	public void addPropertyChangeListener(PropertyChangeListener listener) {
		mPcs.addPropertyChangeListener(listener);
	}

	public void removePropertyChangeListener(PropertyChangeListener listener) {
		mPcs.removePropertyChangeListener(listener);
	}
}
